<?php
class kst_sttb_model extends MY_Model {
	var $model = 'kst_pembelian';
	
	function GetJSON()
	{
		/*$ls_sql	= "	SELECT a.no_po,a.no_sttb,TO_CHAR(a.tgl_order,'DD/MM/YYYY') tgl_order, TO_CHAR(a.tgl_terima,'DD/MM/YYYY') tgl_terima,TO_CHAR(a.tgl_jatuh_tempo,'DD/MM/YYYY') tgl_jatuh_tempo,a.kd_vendor,a.jenis_pembayaran,c.nama_vendor 
		FROM kst_pembelian a JOIN kst_vendor c ON a.kd_vendor = c.kd_vendor
		WHERE	(a.no_po LIKE '%".post('keyword')."%')
		ORDER BY ".(post('sort')=='' ? 'a.no_po' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
		return json_encode( $this->paging($ls_sql) );*/

		$ls_sql	= "	SELECT a.or_po_no, a.or_sttb_no, a.or_sttb_date, a.or_sttb_faktur, a.or_sttb_pembayaran, a.or_sttb_jatuhtempo, a.sup, a.or_po_receivebefore, a.or_status, a.or_po_date, a.contact_id_sttb
		FROM kst_order_receive a JOIN kst_order_receive_details c ON a.or_id = c.or_id
		WHERE	(a.or_po_no LIKE '%".post('keyword')."%')
		ORDER BY ".(post('sort')=='' ? 'a.or_po_no' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));

		return json_encode( $this->paging($ls_sql) );
	}

	function GetForExcel($no_po) 
	{
		$ls_sql	= "	SELECT	a.or_id, a.no_po,a.urutan,a.qty_order,a.kd_barang,b.nama_barang,a.no_sttb,a.qty_terima,a.harga 
					FROM	kst_pembelian_detail a
							LEFT JOIN kst_barang b ON a.kd_barang = b.kd_barang
					WHERE	a.no_po = '".$no_po."' 
					ORDER BY urutan";

		$ls_sql2	= "	SELECT	COUNT(1)
					FROM	kst_pembelian_detail a
							LEFT JOIN kst_barang b ON a.kd_barang = b.kd_barang
					WHERE	a.no_po = '".$no_po."'";
		
		$total = $this->get_data($ls_sql2);

		$result = $this->db->query($ls_sql);

		$arr_response = array(	'rows'=> $result->result_array(), 
								'total' => $total);

		return $arr_response;
	}
		
	function Add($arr_data){
		$this->db->insert('kst_order_receive', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('kst_order_receive', $arr_data, $arr_rule);	
		
	}
	
	function GetSingle($as_pkmaster){
		$result = $this->db->get_where('kst_pembelian', array('no_po' => $as_pkmaster));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_pkmaster)
	{

		$result	= $this->db->query("SELECT a.or_po_no, a.or_sttb_no, TO_CHAR(TO_DATE(a.or_sttb_date,'MM/DD/YYYY'),'DD/MM/YYYY') or_sttb_date, a.or_sttb_faktur, a.or_sttb_pembayaran,TO_CHAR(TO_DATE(a.or_sttb_jatuhtempo,'MM/DD/YYYY'),'DD/MM/YYYY') or_sttb_jatuhtempo, a.sup, a.or_po_receivebefore, a.or_status,TO_CHAR(TO_DATE(a.or_po_date,'MM/DD/YYYY'),'DD/MM/YYYY') or_po_date, a.contact_id_sttb
		FROM kst_order_receive a WHERE a.or_po_no = '".$as_pkmaster."'");
		
		/*$result	= $this->db->query("SELECT a.or_po_no, a.or_sttb_no, a.or_sttb_date, a.or_sttb_faktur, a.or_sttb_pembayaran, a.or_sttb_jatuhtempo, a.sup, a.or_po_receivebefore, a.or_status, a.or_po_date, a.contact_id_sttb
		FROM kst_order_receive a JOIN kst_order_receive_details c ON a.or_id = c.or_id WHERE a.or_po_no = '".$as_pkmaster."'");
		*/
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			$this->db->delete('kst_pembelian_detail', $arr_rule);
			$this->db->delete('kst_pembelian', $arr_rule);			
			
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>